//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AlphaFinder.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/Models/AlphaForm.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::CAlphaFinder (Cephei::QL::Models::Marketmodels::Models::IAlphaForm^ parametricform) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CAlphaForm^ _Cparametricform;
    try
    {
#ifdef HANDLE
        _phAlphaFinder = NULL;
#endif
        _Cparametricform = safe_cast<CAlphaForm^> (parametricform);
        _Cparametricform->Lock();
        boost::shared_ptr<QuantLib::AlphaForm> _parametricform = static_cast<boost::shared_ptr<QuantLib::AlphaForm>> (_Cparametricform->GetShared ()); 
        _ppAlphaFinder = new boost::shared_ptr<QuantLib::AlphaFinder> (new QuantLib::AlphaFinder ( _parametricform ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cparametricform != nullptr) _Cparametricform->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::CAlphaFinder (boost::shared_ptr<QuantLib::AlphaFinder>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAlphaFinder = NULL;
#endif
	_ppAlphaFinder = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::CAlphaFinder (QuantLib::AlphaFinder& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAlphaFinder = NULL;
#endif
	_ppAlphaFinder = new boost::shared_ptr<QuantLib::AlphaFinder> (&childNative);
    
    _AlphaFinderOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::CAlphaFinder (CAlphaFinder^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAlphaFinder = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAlphaFinder = new boost::shared_ptr<QuantLib::AlphaFinder> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::CAlphaFinder (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAlphaFinder = NULL;
#endif
	if (!t->IsSubclassOf(CAlphaFinder::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::CAlphaFinder (QuantLib::Handle<QuantLib::AlphaFinder>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAlphaFinder = &childNative;
	_ppAlphaFinder = &static_cast<boost::shared_ptr<QuantLib::AlphaFinder>>(childNative.currentLink());
    
    _AlphaFinderOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::CAlphaFinder (QuantLib::Handle<QuantLib::AlphaFinder> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAlphaFinder = &childNative;
	_ppAlphaFinder = &static_cast<boost::shared_ptr<QuantLib::AlphaFinder>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::CAlphaFinder (QuantLib::AlphaFinder childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAlphaFinder = NULL;
#endif
	_ppAlphaFinder = new boost::shared_ptr<QuantLib::AlphaFinder> (new QuantLib::AlphaFinder (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::~CAlphaFinder ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAlphaFinder != NULL)
    {
	    delete _ppAlphaFinder;
        _ppAlphaFinder = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::!CAlphaFinder ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAlphaFinder != NULL)
    {
	    delete _ppAlphaFinder;
    }
}
QuantLib::AlphaFinder& Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::GetReference ()
{
    if (_ppAlphaFinder == NULL) throw REFNEW NativeNullException ();
	return **_ppAlphaFinder;
}
boost::shared_ptr<QuantLib::AlphaFinder>& Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::GetShared ()
{
    if (_ppAlphaFinder == NULL) throw REFNEW NativeNullException ();
	return *_ppAlphaFinder;
}
QuantLib::AlphaFinder* Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::GetPointer ()
{
    if (_ppAlphaFinder == NULL) throw REFNEW NativeNullException ();
	return &**_ppAlphaFinder;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AlphaFinder>& Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::GetHandle ()
{
	if (_phAlphaFinder == NULL)
	{
		_phAlphaFinder = new Handle<QuantLib::AlphaFinder> (*_ppAlphaFinder);
	}
	return *_phAlphaFinder;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::HasNative () 
{
	return (_ppAlphaFinder != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::Solve (Double alpha0, Int32 stepindex, Cephei::Core::IVector<Double>^ rateonevols, Cephei::Core::IVector<Double>^ ratetwohomogeneousvols, Cephei::Core::IVector<Double>^ correlations, Double w0, Double w1, Double targetVariance, Double tolerance, Double alphaMax, Double alphaMin, Int32 steps, Double alpha, Double a, Double b, Cephei::Core::IVector<Double>^ ratetwovols)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _alpha0 = (QuantLib::Real)ValueHelper::Convert (alpha0); //a
        QuantLib::Integer _stepindex = (QuantLib::Integer)ValueHelper::Convert (stepindex); //a
        rateonevols->Lock();
        INativeVector<Double>^ _NCIrateonevols = rateonevols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateonevols = safe_cast<CDoubleVector^>(_NCIrateonevols);
        std::vector<QuantLib::Volatility>& _rateonevols = static_cast<std::vector<QuantLib::Volatility>&> (_NCrateonevols->GetReference ());
        ratetwohomogeneousvols->Lock();
        INativeVector<Double>^ _NCIratetwohomogeneousvols = ratetwohomogeneousvols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCratetwohomogeneousvols = safe_cast<CDoubleVector^>(_NCIratetwohomogeneousvols);
        std::vector<QuantLib::Volatility>& _ratetwohomogeneousvols = static_cast<std::vector<QuantLib::Volatility>&> (_NCratetwohomogeneousvols->GetReference ());
        correlations->Lock();
        INativeVector<Double>^ _NCIcorrelations = correlations->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcorrelations = safe_cast<CDoubleVector^>(_NCIcorrelations);
        std::vector<QuantLib::Real>& _correlations = static_cast<std::vector<QuantLib::Real>&> (_NCcorrelations->GetReference ());
        QuantLib::Real _w0 = (QuantLib::Real)ValueHelper::Convert (w0); //a
        QuantLib::Real _w1 = (QuantLib::Real)ValueHelper::Convert (w1); //a
        QuantLib::Real _targetVariance = (QuantLib::Real)ValueHelper::Convert (targetVariance); //a
        QuantLib::Real _tolerance = (QuantLib::Real)ValueHelper::Convert (tolerance); //a
        QuantLib::Real _alphaMax = (QuantLib::Real)ValueHelper::Convert (alphaMax); //a
        QuantLib::Real _alphaMin = (QuantLib::Real)ValueHelper::Convert (alphaMin); //a
        QuantLib::Integer _steps = (QuantLib::Integer)ValueHelper::Convert (steps); //a
        QuantLib::Real _alpha = (QuantLib::Real)ValueHelper::Convert (alpha); //a
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a); //a
        QuantLib::Real _b = (QuantLib::Real)ValueHelper::Convert (b); //a
        ratetwovols->Lock();
        INativeVector<Double>^ _NCIratetwovols = ratetwovols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCratetwovols = safe_cast<CDoubleVector^>(_NCIratetwovols);
        std::vector<QuantLib::Volatility>& _ratetwovols = static_cast<std::vector<QuantLib::Volatility>&> (_NCratetwovols->GetReference ());
    	bool _rv = (bool)(*_ppAlphaFinder)->solve ( _alpha0,  _stepindex,  _rateonevols,  _ratetwohomogeneousvols,  _correlations,  _w0,  _w1,  _targetVariance,  _tolerance,  _alphaMax,  _alphaMin,  _steps,  _alpha,  _a,  _b,  _ratetwovols );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateonevols != nullptr) rateonevols->Unlock(); //NO2  
        if (ratetwohomogeneousvols != nullptr) ratetwohomogeneousvols->Unlock(); //NO2  
        if (correlations != nullptr) correlations->Unlock(); //NO2  
        if (ratetwovols != nullptr) ratetwovols->Unlock(); //NO2  
    }
}
Boolean Cephei::QL::Models::Marketmodels::Models::CAlphaFinder::SolveWithMaxHomogeneity (Double alpha0, Int32 stepindex, Cephei::Core::IVector<Double>^ rateonevols, Cephei::Core::IVector<Double>^ ratetwohomogeneousvols, Cephei::Core::IVector<Double>^ correlations, Double w0, Double w1, Double targetVariance, Double tolerance, Double alphaMax, Double alphaMin, Int32 steps, Double alpha, Double a, Double b, Cephei::Core::IVector<Double>^ ratetwovols)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _alpha0 = (QuantLib::Real)ValueHelper::Convert (alpha0); //a
        QuantLib::Integer _stepindex = (QuantLib::Integer)ValueHelper::Convert (stepindex); //a
        rateonevols->Lock();
        INativeVector<Double>^ _NCIrateonevols = rateonevols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateonevols = safe_cast<CDoubleVector^>(_NCIrateonevols);
        std::vector<QuantLib::Volatility>& _rateonevols = static_cast<std::vector<QuantLib::Volatility>&> (_NCrateonevols->GetReference ());
        ratetwohomogeneousvols->Lock();
        INativeVector<Double>^ _NCIratetwohomogeneousvols = ratetwohomogeneousvols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCratetwohomogeneousvols = safe_cast<CDoubleVector^>(_NCIratetwohomogeneousvols);
        std::vector<QuantLib::Volatility>& _ratetwohomogeneousvols = static_cast<std::vector<QuantLib::Volatility>&> (_NCratetwohomogeneousvols->GetReference ());
        correlations->Lock();
        INativeVector<Double>^ _NCIcorrelations = correlations->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcorrelations = safe_cast<CDoubleVector^>(_NCIcorrelations);
        std::vector<QuantLib::Real>& _correlations = static_cast<std::vector<QuantLib::Real>&> (_NCcorrelations->GetReference ());
        QuantLib::Real _w0 = (QuantLib::Real)ValueHelper::Convert (w0); //a
        QuantLib::Real _w1 = (QuantLib::Real)ValueHelper::Convert (w1); //a
        QuantLib::Real _targetVariance = (QuantLib::Real)ValueHelper::Convert (targetVariance); //a
        QuantLib::Real _tolerance = (QuantLib::Real)ValueHelper::Convert (tolerance); //a
        QuantLib::Real _alphaMax = (QuantLib::Real)ValueHelper::Convert (alphaMax); //a
        QuantLib::Real _alphaMin = (QuantLib::Real)ValueHelper::Convert (alphaMin); //a
        QuantLib::Integer _steps = (QuantLib::Integer)ValueHelper::Convert (steps); //a
        QuantLib::Real _alpha = (QuantLib::Real)ValueHelper::Convert (alpha); //a
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a); //a
        QuantLib::Real _b = (QuantLib::Real)ValueHelper::Convert (b); //a
        ratetwovols->Lock();
        INativeVector<Double>^ _NCIratetwovols = ratetwovols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCratetwovols = safe_cast<CDoubleVector^>(_NCIratetwovols);
        std::vector<QuantLib::Volatility>& _ratetwovols = static_cast<std::vector<QuantLib::Volatility>&> (_NCratetwovols->GetReference ());
    	bool _rv = (bool)(*_ppAlphaFinder)->solveWithMaxHomogeneity ( _alpha0,  _stepindex,  _rateonevols,  _ratetwohomogeneousvols,  _correlations,  _w0,  _w1,  _targetVariance,  _tolerance,  _alphaMax,  _alphaMin,  _steps,  _alpha,  _a,  _b,  _ratetwovols );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateonevols != nullptr) rateonevols->Unlock(); //NO2  
        if (ratetwohomogeneousvols != nullptr) ratetwohomogeneousvols->Unlock(); //NO2  
        if (correlations != nullptr) correlations->Unlock(); //NO2  
        if (ratetwovols != nullptr) ratetwovols->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::IAlphaFinder^ Cephei::QL::Models::Marketmodels::Models::CAlphaFinder_Factory::Create (Cephei::QL::Models::Marketmodels::Models::IAlphaForm^ parametricform)
{
    return REFNEW CAlphaFinder ( parametricform);
}
